/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.api.R;
import org.springblade.modules.repair.entity.MaintainMalfunctionLog;
import org.springblade.modules.repair.service.IMaintainMalfunctionLogService;
import org.springblade.modules.repair.vo.MaintainMalfunctionLogListVO;
import org.springblade.modules.repair.vo.MaintainMalfunctionLogVO;
import org.springblade.modules.repair.vo.WorkTrackVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 故障维修记录 控制器
 *
 * @author BladeX
 * @since 2021-05-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("repair/maintain/malfunction/log")
@Api(value = "任务计划跟踪信息管理", tags = "任务计划跟踪信息管理")
public class MaintainMalfunctionLogController extends BladeController {

	private final IMaintainMalfunctionLogService maintainMalfunctionLogService;

	/*代表维保端*/
	private final static Integer STATUS = 1;

	/**
	 * 分页 故障维修记录
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "计划跟踪信息", notes = "传入planId")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "planId", value = "任务计划id", required = true)
	})
	public R list(@RequestParam("planId") Long planId) {
		List<MaintainMalfunctionLog> list = maintainMalfunctionLogService.list(Wrappers.<MaintainMalfunctionLog>lambdaQuery().eq(MaintainMalfunctionLog::getPlanId, planId).eq(MaintainMalfunctionLog::getStatus, STATUS).orderByDesc(MaintainMalfunctionLog::getCreateTime));
		List<MaintainMalfunctionLogVO> resultList = new ArrayList<>(16);
		list.forEach(maintainMalfunctionLog -> {
			MaintainMalfunctionLogVO maintainMalfunctionLogVO = new MaintainMalfunctionLogVO();
			BeanUtil.copyProperties(maintainMalfunctionLog, maintainMalfunctionLogVO);
			maintainMalfunctionLogVO.setCreateTime(DateUtil.format(maintainMalfunctionLog.getCreateTime(), "HH:mm"));
			maintainMalfunctionLogVO.setCreateDate(DateUtil.format(maintainMalfunctionLog.getCreateTime(), "MM-dd"));
			resultList.add(maintainMalfunctionLogVO);
		});
		Map<String, List<MaintainMalfunctionLogVO>> map = resultList.stream().collect(Collectors.groupingBy(MaintainMalfunctionLogVO::getCreateDate));
		List<MaintainMalfunctionLogVO> voList = map.get(DateUtil.format(new Date(), "MM-dd"));
		if (voList != null && voList.size() > 0) {
			voList.forEach(o -> o.setCreateDate(null));
			map.remove(DateUtil.format(new Date(), "MM-dd"));
			map.put("今天", voList);
		}
		TreeMap<String, List<MaintainMalfunctionLogVO>> resultMap = new TreeMap(map);
		List<MaintainMalfunctionLogListVO> collect = resultMap.entrySet().stream().map(e -> new MaintainMalfunctionLogListVO(e.getKey(), e.getValue())).collect(Collectors.toList());
		return R.data(collect);
	}

	@TenantIgnore
	@GetMapping("/selectWorkTracks")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "任务追踪记录")
	public R<WorkTrackVO> selectWorkTracks(@ApiParam(value = "计划id", required = true) @RequestParam Long planId) {
		return R.data(maintainMalfunctionLogService.selectWorkTracks(planId));
	}

}
